package com.xyy.saas.payment.adpater.jdCredit.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.Objects;

/**
 * 时间工具
 *
 * @author yuchengying
 * @date 2023/12/19
 */
@Slf4j
public class DateUtil {

    /**
     * 日期格式
     */
    public static final String DATEPATTERN = "yyyyMMddHHmmss";

    public static final String DATE_PATTERN = "yyyy-MM-dd HH:mm:ss";

    public static final String YEAR_MOUTH_DAY = "yyyy-MM-dd";


    public static Date dayStart(Date date) {
        if (Objects.isNull(date)) {
            return null;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();
    }

    public static Date dayEnd(Date date) {
        if (Objects.isNull(date)) {
            return null;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        return calendar.getTime();
    }

    public static String now() {
        SimpleDateFormat STD_FMT = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
        return fmt(new Date(), STD_FMT);
    }

    public static String fileName() {
        SimpleDateFormat FILE_NAME = new SimpleDateFormat("yyyyMMddHHmmss");
        return fmt(new Date(), FILE_NAME);
    }
    /**
     * 字符串转换成日期
     *
     * @param dateStr 时间str
     * @param type 类型
     * @return {@link Date }
     */
    public static Date getDate(String dateStr, String type){
        if(Objects.isNull(dateStr)){
            return null;
        }
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(type);
        LocalDate parse = LocalDate.parse(dateStr, formatter);
        ZonedDateTime zonedDateTime = parse.atStartOfDay(ZoneId.systemDefault());
        Instant instant = zonedDateTime.toInstant();
        return Date.from(instant);
    }

    /**
     * 查询时间通过类型
     *
     * @param dateStr 时间str
     * @param pattern
     * @return {@link Date }
     */
    public static Date getDateByPattern(String dateStr, String pattern) {
        DateFormat dateFormat = new SimpleDateFormat(pattern);
        if (StringUtils.isBlank(dateStr)) {
            return null;
        }
        try {
            return dateFormat.parse(dateStr);
        } catch (ParseException e) {
           log.info("时间转换异常：{}", e.getMessage());
        }
        return null;
    }

    private static String fmt(Date date, DateFormat fmt) {
        return fmt.format(date);
    }

    public static String formatDateString(Date date, String form) {
        if (date == null) {
            return null;
        }
        SimpleDateFormat format = new SimpleDateFormat(form);
        return format.format(date);
    }

}
